#!groovy  buildPipeline.groovy
import com.jenkins.devops.Constants

def call(Map kwArgs = [:]) {
    build(kwArgs)
}
//整体流程，所有stage的运行与否都取决于yaml中定义
def build(Map kwArgs = [:]) {
    def buildType = "DEV"
    def artifact
    def tag = 'latest'
    List emailList
    try {
        node("build-jdk8") {
            stage("Checkout") {
                checkout scm
                artifact = loadConfiguration(filePath: 'jenkins.yaml')
                emailList = artifact.notificationEmails.join(',')
            }
            stage("Unit Test") {
                if (artifact.enableUnitTest) {
                    println("unit Test")
                }
            }
            stage("Cucumber Test") {
                if (artifact.enableCucumberTest) {
                    println("Cucumber Test")
                }
            }
            stage("Build DockerFile") {
                if (artifact.needDockerImage) {
                    def dockerName = artifact.projectName
                    for (Map imageMap in artifact.dockerImages) {
                        def dockerfile = imageMap['imageMap']
                        def path = imageMap['path']
                        sh "docker build -t $Constants.DockerLocalPrefix/$dockerName:$tag -f $dockerfile"
                        sh "docker push"
                    }
                }
            }
            stage("Deploy") {
                println ("deploy")
            }
        }
    } catch (error) {
        currentBuild.result = "$Constants.FAILURE"
        throw error
    } finally {
        notifyBuild(
                toList: emailList,
                buildType: buildType
        )
    }
} 